package com.sx.gongzhong.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.MyApplication;
import com.sx.gongzhong.R;
import com.sx.gongzhong.calender.listener.OnPagerChangeListener;
import com.sx.gongzhong.calender.utils.CalendarUtil;
import com.sx.gongzhong.calender.weiget.CalendarView;
import com.sx.gongzhong.model.CheckInBean;
import com.sx.gongzhong.model.CheckRecords;
import com.sx.gongzhong.model.MsgEvent;
import com.sx.gongzhong.model.UserInfoBean;
import com.sx.gongzhong.net.MyRxBus;
import com.sx.gongzhong.pest.CheckInPest;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.CheckInPestView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import xiaofei.library.datastorage.DataStorageFactory;

/**
 * 签到
 * Created by admin on 2017/12/29.
 */

public class CheckInActivity extends BaseActivity implements CheckInPestView {
    @BindView(R.id.tv_month_count)
    TextView tvMonthCount;
    @BindView(R.id.tv_week_count)
    TextView tvWeekCount;
    @BindView(R.id.calendar)
    CalendarView calendar;
    @BindView(R.id.tv_current_date)
    TextView tvCurrentDate;
    @BindView(R.id.tv_myIntegral)
    TextView tvMyIntegral;
    private boolean isChange = false;
    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_checkin);
        ButterKnife.bind(this);
        setTitleName("签到");
//        setTitleRight("说明");
        tvMyIntegral.setText(getIntent().getStringExtra("integral"));

        int[] currentDate = CalendarUtil.getCurrentDate();
        calendar.setInitDate(currentDate[0] + "." + currentDate[1])
                .init();
        tvCurrentDate.setText(currentDate[0] + "年" + currentDate[1] + "月");
        new CheckInPest(CheckInActivity.this).setCheckIn(CheckInActivity.this);
    }

    @Override
    public void showError(String message) {
        if ("签到失败".equals(message)) {
            int[] currentDate = CalendarUtil.getCurrentDate();
            new CheckInPest(CheckInActivity.this).getCheckInCount(currentDate[0]  +"-"+ (currentDate[1] < 10 ? "0"+currentDate[1] : currentDate[1])  + "");
        } else {
            ToastUtils.showToast(CheckInActivity.this, message);
        }
    }

    @Override
    public void setCheckInSuccess(CheckRecords data) {//签到成功显示弹框
//        View view = LayoutInflater.from(CheckInActivity.this).inflate(R.layout.checkin_success, null);
//        final PopupWindow popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, false);
//        view.findViewById(R.id.iv_checkin_close).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                popupWindow.dismiss();
//            }
//        });
//        popupWindow.showAtLocation(tvMonthCount, 0, 0, Gravity.CENTER);
        MyApplication.instance.userBean.lastCheckInDate = data.signInTime.split(" ")[0];
        DataStorageFactory.getInstance(getApplicationContext(), DataStorageFactory.TYPE_DATABASE)
                .storeOrUpdate(MyApplication.instance.userBean,getPackageName());
        MyRxBus.get().post(new MsgEvent("MyFragment", 3, null));
        int[] currentDate = CalendarUtil.getCurrentDate();
        new CheckInPest(CheckInActivity.this).getCheckInCount(currentDate[0] +"-"+ (currentDate[1] < 10 ? "0"+currentDate[1] : currentDate[1]) + "");
    }

    @Override
    public void getCheckInCount(CheckInBean data) {
        if (data != null) {
            if (isChange){
                if (data.records != null && data.records.size() > 0) {
                    List<String> selectDate = new ArrayList<>();
                    for (CheckRecords recordBean : data.records) {
                        String[] date = recordBean.signInTime.split(" ");
                        String[] s = date[0].split("-");
                        selectDate.add(s[0] + "." + s[1] + "." + s[2]);
                    }
                    calendar.refreshMonthView(selectDate);
                }
            }else {
                initView(data);
            }
        }
    }

    private void initView(CheckInBean data) {
        tvMonthCount.setText("本月签到" + data.monthCount + "天");
        tvWeekCount.setText("本周签到" + data.weekCount + "天");
        changeText(tvMonthCount);
        changeText(tvWeekCount);
        if (data.records != null && data.records.size() > 0) {
            int[] currentDate = CalendarUtil.getCurrentDate();
            List<String> selectDate = new ArrayList<>();
            for (CheckRecords recordBean : data.records) {
                String[] date = recordBean.signInTime.split(" ");
                String[] s = date[0].split("-");
                selectDate.add(s[0] + "." + s[1] + "." + s[2]);
            }
            calendar.setInitDate(currentDate[0] + "." + currentDate[1])
                    .setDefaultDate(selectDate)
                    .init();
        }
        calendar.setOnPagerChangeListener(new OnPagerChangeListener() {
            @Override
            public void onPagerChanged(int[] date) {
                tvCurrentDate.setText(date[0] + "年" + date[1] + "月");
                isChange = true;
                new CheckInPest(CheckInActivity.this).getCheckInCount(date[0] +"-"+ (date[1] < 10 ? "0"+date[1] : date[1]) + "");
            }
        });
    }

    private void changeText(TextView tv) {
        Spannable span = new SpannableString(tv.getText());
        span.setSpan(new AbsoluteSizeSpan(51), 4, tv.getText().length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new ForegroundColorSpan(Color.BLACK), 4, tv.getText().length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(span);
    }
}
